package com.wgts.wgts.mvp.view.activity.myorder;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.dialog.ToastDate;
import com.wgts.wgts.control.dialog.ToastDialogFragment;
import com.wgts.wgts.control.recycleview.BaseLoadMoreAdapter;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.localbean.GoodsData;
import com.wgts.wgts.mvp.presenter.IMyOrderFragmentPersenter;
import com.wgts.wgts.netmodle.resp.MyOrderListEntity;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyOrderAdapter extends BaseLoadMoreAdapter<MyOrderAdapter.MyOrderViewHolder> {
    private OrderItemRecycleAdapter itemRecycleAdapter;
    private CustomLineaLayoutManager linearLayoutManager;

    private Activity context;

    private List<MyOrderListEntity> list;

    private ArrayList<MyOrderViewHolder> viewHolders;

    private FragmentManager manager;

    private IMyOrderFragmentPersenter presenter;

    private onItemClickListen onItemClickListen;

    private int goodSize = 0;

    public MyOrderAdapter(Activity context, List<MyOrderListEntity> list, FragmentManager manager
            , IMyOrderFragmentPersenter presenter) {
        super(context);
        this.context = context;
        this.list = list;
        viewHolders = new ArrayList<>();
        this.manager = manager;
        this.presenter = presenter;
    }

    @Override
    public void onBindBodyViewHolder(@NonNull MyOrderViewHolder viewHolder, final int i) {
        String orderNumber = "订单号：" + list.get(i).orderCode;
        Spannable span = new SpannableString(orderNumber);
        span.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.title_text)), 4, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.orderNumber.setText(span);
        viewHolder.productName.setText(list.get(i).shops.get(0).products.get(0).productName);
        goodSize = 0;
        for (MyOrderListEntity.Shop product : list.get(i).shops) {

            if (product.products.size() > 0) {
                goodSize += product.products.get(0).productCount;
            }
        }
//        viewHolder.orderAllAmount.setText("共" + goodSize + "件，计:");
        viewHolder.orderAllPrice.setText("￥" + list.get(i).orderTruePrice);
        viewHolder.orderTimeText.setText(list.get(i).createTime);
        viewHolder.shoppingCarTypeShow.setText(list.get(i).shops.get(0).products.get(0).productStandard);
//        if (AppInfor.getInstance().getUserState() < 2) {
//            viewHolder.canMadeLy.setVisibility(View.INVISIBLE);
//        } else {
//            viewHolder.canMadeLy.setVisibility(View.VISIBLE);
//            viewHolder.canMadeText.setText("￥" + list.get(i).commissionProfit);
//        }
        if (list.get(i).orderTradeState.equals("CLOSED")) {
            viewHolder.orderStatus.setText("已关闭");
            viewHolder.orderPayLy.setVisibility(View.GONE);
            viewHolder.orderCancelLy.setVisibility(View.GONE);
        } else if (list.get(i).orderTradeState.equals("COMPLETED")) {
            viewHolder.orderStatus.setText("已完成");
            viewHolder.orderPayLy.setVisibility(View.GONE);
            viewHolder.orderCancelLy.setVisibility(View.GONE);
        } else {
            switch (list.get(i).orderState) {
                case "UNPAID":
                    viewHolder.orderStatus.setText("待付款");
                    viewHolder.orderPayLy.setVisibility(View.VISIBLE);
                    viewHolder.orderCancelLy.setVisibility(View.VISIBLE);
                    viewHolder.orderCancelLy.setBackgroundResource(R.drawable.wgts_login_border);
                    viewHolder.leftButtonText.setTextColor(ContextCompat.getColor(context, R.color.hint_6));
                    viewHolder.rightButtonText.setText("去支付");
                    viewHolder.leftButtonText.setText("取消订单");
                    break;
                case "UNDELIVER":
                    //待发货状态,与待收货合并。
                    viewHolder.orderStatus.setText("待发货");
                    viewHolder.orderPayLy.setVisibility(View.GONE);
                    viewHolder.orderCancelLy.setVisibility(View.GONE);
                    viewHolder.rightButtonText.setText("确认收货");
                    break;
                case "UNRECEIPT":
                    viewHolder.orderStatus.setText("待收货");
                    viewHolder.orderPayLy.setVisibility(View.VISIBLE);
                    viewHolder.orderCancelLy.setVisibility(View.GONE);
                    viewHolder.rightButtonText.setText("确认收货");
                    break;
                case "UNEVALUATED":
                    viewHolder.orderStatus.setText("待评价");
                    viewHolder.orderPayLy.setVisibility(View.VISIBLE);
                    viewHolder.orderCancelLy.setVisibility(View.VISIBLE);
                    viewHolder.orderCancelLy.setBackgroundResource(R.drawable.wgts_submit_border);
                    viewHolder.leftButtonText.setTextColor(ContextCompat.getColor(context, R.color.white));
                    viewHolder.rightButtonText.setText("立即评价");
                    viewHolder.leftButtonText.setText("申请售后");
                    break;
                case "COMPLETED":
                    viewHolder.orderStatus.setText("已完成");
                    viewHolder.orderPayLy.setVisibility(View.GONE);
                    viewHolder.orderCancelLy.setVisibility(View.VISIBLE);
                    viewHolder.orderCancelLy.setBackgroundResource(R.drawable.wgts_submit_border);
                    viewHolder.leftButtonText.setTextColor(ContextCompat.getColor(context, R.color.white));
                    viewHolder.leftButtonText.setText("申请售后");
                    break;
                case "RETURN_GOOD":
                    viewHolder.orderStatus.setText("退换货");
                    viewHolder.orderPayLy.setVisibility(View.VISIBLE);
                    viewHolder.orderCancelLy.setVisibility(View.GONE);
                    viewHolder.rightButtonText.setText("查看进度");
                    break;
            }
        }


        MoHuGlide.getInstance().glideImgsFormList(viewHolder.imgProduct, list.get(i).shops.get(0).products.get(0).productSmallImage, context);

//        itemRecycleAdapter.setOnClickListen(new OrderItemRecycleAdapter.onClickListen() {
//            @Override
//            public void onclick() {
//                if (onItemClickListen != null) {
//                    onItemClickListen.onClick(String.valueOf(list.get(i).orderId), list.get(i).orderTradeState);
//                }
//            }
//        });


        //取消订单
        viewHolder.orderCancely.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (list.get(i).orderState) {
                    case "UNPAID":
                        cancelOrder(i);
                        break;
                    case "UNEVALUATED":
                        applyForAfterSell(i);
                        break;
                    case "COMPLETED":
                        applyForAfterSell(i);
                        break;
                }
            }
        });
        //立即付款
        viewHolder.orderPayly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (list.get(i).orderState) {
                    case "UNPAID":
                        goPay(i);
                        break;
                    case "UNDELIVER":
                        affirmTakeGoods(i);
                        break;
                    case "UNRECEIPT":
                        affirmTakeGoods(i);
                        break;
                    case "UNEVALUATED":
                        goEvaluate(i);
                        break;
                    case "RETURN_GOOD":
                        watchProgress(i);
                        break;
                }
            }
        });
        //商品详情
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListen != null) {
                    onItemClickListen.onClick(String.valueOf(list.get(i).orderId), list.get(i).orderTradeState);
                }
            }
        });
    }

    @NonNull
    @Override
    public MyOrderViewHolder onCreateBodyViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyOrderViewHolder(LayoutInflater.from(context).inflate(R.layout.my_order_recycle_item, viewGroup, false));
    }


    @Override
    public int getBodyItemCount() {
        return list.size();
    }

    @Override
    public int getBodyItemViewType(int position) {
        return 0;
    }

    public class MyOrderViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.order_number)
        TextView orderNumber;
        @BindView(R.id.order_status)
        TextView orderStatus;
        @BindView(R.id.img_product)
        ImageView imgProduct;
        @BindView(R.id.product_name)
        TextView productName;
        @BindView(R.id.shopping_car_type_show)
        TextView shoppingCarTypeShow;
        @BindView(R.id.order_all_amount)
        TextView orderAllAmount;
        @BindView(R.id.order_all_price)
        TextView orderAllPrice;
        @BindView(R.id.can_made_text)
        TextView canMadeText;
        @BindView(R.id.can_made_ly)
        LinearLayout canMadeLy;
        @BindView(R.id.wait_pay_ly)
        RelativeLayout waitPayLy;
        @BindView(R.id.order_time_text)
        TextView orderTimeText;
        @BindView(R.id.left_button_Text)
        TextView leftButtonText;
        @BindView(R.id.order_cancel_ly)
        LinearLayout orderCancelLy;
        @BindView(R.id.right_button_text)
        TextView rightButtonText;
        @BindView(R.id.order_pay_ly)
        LinearLayout orderPayLy;

        public TextView tv_hous, tv_minute, tv_second;


        public LinearLayout orderCancely, orderPayly, watchOrderDetailsly;

        private View itemView;

        public MyOrderViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);


            orderCancely = itemView.findViewById(R.id.order_cancel_ly);
            orderPayly = itemView.findViewById(R.id.order_pay_ly);
            this.itemView = itemView;
        }
    }

    //取消订单方法
    private void cancelOrder(final Integer position) {
        ToastDate date = new ToastDate();
        date.setCancelQuestion("确认取消订单？");
        date.setPointText("我再想想");
        date.setCancelText("确认取消");
        final ToastDialogFragment toastDialogFragment = ToastDialogFragment.setToastMessage(date);
        toastDialogFragment.show(manager, "orderCancelDialog");
        toastDialogFragment.setOnClickListener(new ToastDialogFragment.OnClickListener() {
            @Override
            public void onClickCancle() {
                toastDialogFragment.dismiss();
            }

            @Override
            public void onClickEnsuer() {
                presenter.cancelOrder(String.valueOf(list.get(position).orderId), position);
                toastDialogFragment.dismiss();
            }
        });
    }

    //确认收货方法
    private void affirmTakeGoods(final int position) {
        ToastDate date = new ToastDate();
        date.setCancelQuestion("是否确认收货？");
        date.setPointText("确认");
        date.setCancelText("取消");
        final ToastDialogFragment toastDialogFragment = ToastDialogFragment.setToastMessage(date);
        toastDialogFragment.show(manager, "orderCancelDialog");
        toastDialogFragment.setOnClickListener(new ToastDialogFragment.OnClickListener() {
            @Override
            public void onClickCancle() {
                presenter.takeGoods(String.valueOf(list.get(position).orderId));
                toastDialogFragment.dismiss();
            }

            @Override
            public void onClickEnsuer() {
                toastDialogFragment.dismiss();
            }
        });
    }

    //去支付方法
    private void goPay(int position) {
        presenter.goPay(String.valueOf(list.get(position).orderId), list.get(position).orderTruePrice,
                list.get(position).orderType, list.get(position).commissionProfit, list.get(position).orderCode);
    }

    //申请售后方法
    private void applyForAfterSell(int position) {
        GoodsData goodsData = new GoodsData();
        goodsData.setCount("x1");
        goodsData.setUrl(list.get(position).shops.get(0).products.get(0).productSmallImage);
        goodsData.setProductName(list.get(position).shops.get(0).products.get(0).productName);
        goodsData.setProductPrice("￥" + list.get(position).shops.get(0).products.get(0).productPrice);
        goodsData.setProductType(list.get(position).shops.get(0).products.get(0).productStandard);
        presenter.applyForAfterSell(String.valueOf(list.get(position).orderId), list.get(position).orderCode, goodsData);
    }

    //立即评价方法
    private void goEvaluate(int position) {
        presenter.goEvaluate(String.valueOf(list.get(position).orderId));
    }

    //查看进度方法
    private void watchProgress(int position) {
        Intent intent = new Intent(context, MyOrderDetailsActivity.class);
        intent.putExtra(MyOrderDetailsActivity.ORDER_ID, String.valueOf(list.get(position).orderId));
        intent.putExtra(MyOrderDetailsActivity.PAY_STATUS, list.get(position).orderTradeState);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    public interface onItemClickListen {
        void onClick(String orderId, String payStatus);
    }

    public void setOnItemClickListen(onItemClickListen onItemClickListen) {
        this.onItemClickListen = onItemClickListen;
    }


}
